var api = require('../../util/api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: api.page(10),
    userList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.onReachBottom();
  },
  onShow() {
    if(api.isRefresh()){
      this.setData({
        userList: []
      });
      api.refresh(this.options, this.onLoad);
    }
  },
  toEdit: function (e) {
    var _this = this;
    let userId = e.currentTarget.dataset.userId;
    wx.navigateTo({
      url: `./info?userId=${userId}&roleKey=${_this.options.roleKey}`
    });
  },
  onReachBottom(e) {
    var _this = this;
    var page = _this.data.page;
    api.get(api.url.listUserByRoleKey, {
        roleKey: _this.options.roleKey
    },page).then(res => {
      let hasData = true;
      if (api.isBlank(res.rows)) {
        hasData = false;
      }
      page.setTotal(res.total);
      _this.setData({
        userList: _this.data.userList.concat(res.rows),
        hasData: hasData
      });
    })
}
})